#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
脚本名称: QQ 自动修炼脚本 v0.13
脚本作者：艾雪 & Bing Copilet
Python 版本: 3.x
编码: UTF-8
使用说明: 
    首次运行前请自行安装依赖 (如在Xshell/Powershell/Terminal中输入以下命令并回车)
    pip install time psutil pyautogui pyperclip win32gui
    该脚本用于自动在 QQ 中发送修炼命令。
    请确保 QQ 已经登录并运行，并打开修仙群聊使得光标处于消息编辑区内。
    使用方法：  python ./Autoxixi_v0.13.py
    运行脚本时会每61秒发送一次修炼命令。
"""
import time
import psutil
import pyautogui as gui
import pyperclip as clip
import win32gui
import random

robot_name = "@汐汐酱"#"@小小"
command_msg = "/修炼"
tupo_msg = "直接突破"
tupo_interval = 5 # 每x次修炼后尝试直接突破
bcast_interval = 100 # 每y次修炼后QQ发送消息
slptime = 0.02  # 操作时间间隔，可修改，部分回车需要额外间隔时间

gui.FAILSAFE = False  # 关闭故障保护


def QQ_Run():
    return any(proc.name() == 'QQ.exe' for proc in psutil.process_iter(['name']))

def QQswitch():
    time.sleep(slptime)
    gui.hotkey("ctrl","alt","x") # QQ默认的“显示/隐藏主面板”，可修改

def AltTab():
    time.sleep(slptime)
    gui.hotkey("alt","tab")

def SendXiulian():
    clip.copy(robot_name)
    gui.hotkey("ctrl", "v")
    time.sleep(10*slptime)
    gui.press("enter")
    clip.copy(command_msg)
    gui.hotkey("ctrl", "v")
    time.sleep(10*slptime)
    clip.copy(" ")
    gui.press("enter")
    time.sleep(slptime)
    gui.press("enter")
    time.sleep(slptime)
    gui.press("enter")
    # gui.hotkey("ctrl", "enter")
    time.sleep(slptime)
    gui.press("enter")
    time.sleep(10*slptime)
    gui.press("enter")
    time.sleep(slptime)
    gui.press("enter")

def SendPo():
    clip.copy(robot_name)
    gui.hotkey("ctrl", "v")
    time.sleep(10*slptime)
    gui.press("enter")
    clip.copy(tupo_msg)
    gui.hotkey("ctrl", "v")
    time.sleep(10*slptime)
    gui.press("enter")
    time.sleep(slptime)
    gui.press("enter")
    time.sleep(10*slptime)
    gui.press("enter")
    time.sleep(slptime)
    gui.press("enter")

def SendCounts(count_num):
    count_msg = f"------ AutoXixi已助您完成 {count_num} 次修炼！------"
    clip.copy(count_msg)
    gui.hotkey("ctrl", "v")
    time.sleep(slptime)
    gui.press("enter")
    time.sleep(slptime)
    gui.press("enter")

def countdown(t):
    while t:
        mins, secs = divmod(t, 60)
        timeformat = '{:02d}:{:02d}'.format(mins, secs)
        print(timeformat, end='\r')
        time.sleep(1)
        t -= 1
    # print("修炼时间到！")

def get_topwindow_title():
    foreground_window_handle = win32gui.GetForegroundWindow()
    # 获取窗口标题
    window_title = win32gui.GetWindowText(foreground_window_handle)
    return window_title

def Xiulian_main(count):
    SendXiulian()
    # if count > 0 and count % tupo_interval == 0:
    #     time.sleep(1)
    #     SendPo()
    # if count > 0 and count % bcast_interval == 0:
    #     time.sleep(1)
    #     SendCounts(count)

def main():
    count = 0
    while QQ_Run():
        try:
            if get_topwindow_title() == "QQ":
                print("QQ is running now...")
                Xiulian_main(count)
            else:
                QQswitch()
                Xiulian_main(count)
                AltTab()               
            # QQswitch()
            # Xiulian_main(count)
            # # QQswitch()
            # AltTab()
            # time.sleep(62)  # 每分钟执行一次，留1~2秒余地
            countdown(61)
            time.sleep(random.random())
            count += 1
            print(f"Xiulian completed for {count} times.")
        except Exception as e:
            print(f"Error occurred: {e}")

if __name__ == "__main__":
    main()
